/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/


#include "keymaze300reader.h"

Keymaze300Reader::Keymaze300Reader()
{
}


void Keymaze300Reader::readFile(QString name)
{
    txt_file_name = name;
    txt_file = new QFile(txt_file_name);
    QString tmp;
    QRegExp new_track_reg(TRACK);

    more_tracks = false;


    if (!txt_file->open(QIODevice::ReadOnly | QIODevice::Text))
        return;

    QTextStream in(txt_file);
    while(!in.atEnd())
    {
        if(!more_tracks)
        {
            tmp = in.readLine();
        }

        if(tmp.count(new_track_reg)>0)
        {
            more_tracks = false;
            readTrack(in);
        }

       /* if(tmp.compare(WAYPOINT) == 0)
        {
            qDebug(tmp.toAscii()+ " - waypoints");
        }

        if(tmp.compare(FILE_CREATION_DATE)== 0)
        {
            qDebug(tmp.toAscii()+" - file creation");
        }

        if(tmp.compare(FILE_TRACKS) == 0)
        {
            qDebug(tmp.toAscii()+" - file tracks");
            readFileTracks(in);
        }*/
    }
    txt_file->close();
}

void Keymaze300Reader::readTrack(QTextStream &in)
{
    int i;
    TrackPoint *t;
    QString tmp;
    QStringList tmp_fields;
    QDateTime time;
    QTime duration;
    int duration_sec;
    QList<TrackPoint *> lst_trk_points;

    /* read track start time*/
    tmp = in.readLine();/* Location : Location=2008-12-23:14:58:26,69925,8407,*/
    tmp_fields = tmp.split("=");
    tmp_fields = tmp_fields.at(1).split(",");
    time = QDateTime::fromString(tmp_fields.at(0),"yyyy-M-d:h:m:s");

    /* read track duration */
    duration_sec = tmp_fields.at(1).toDouble()/10;
    duration.setHMS(0,0,0);
    duration = duration.addSecs(duration_sec);

    /* this parameters aren't used */
    tmp = in.readLine();/* Speed */
    tmp = in.readLine();/* First point */




    QRegExp new_point_reg(N_POINT_REGEXP);
    QRegExp new_track_reg(TRACK);

    /* add track points */
    while(tmp.count(new_point_reg)>0)
    {
        tmp_fields = tmp.split("=");
        tmp_fields = tmp_fields.at(1).split(",");
        t = new TrackPoint();
        t->setValues(tmp_fields.at(0).toFloat()/100000,tmp_fields.at(1).toFloat()/100000,0,time);

        lst_trk_points.append(t);


        tmp = in.readLine();
    }

    /* set time interval to each point... interpolated because source file doesn't specifies it */
    for(i=0;i<lst_trk_points.count();i++)
    {
        lst_trk_points.at(i)->setTime(time.addSecs((i+1)*duration_sec/lst_trk_points.count()));
    }

    /* add track to tracks list */
    lst_trks.append(lst_trk_points);

    /* check if this file is multi-track */
    if(tmp.count(new_track_reg)>0)
    {
        more_tracks = true;
    }
}

QList< QList<TrackPoint *> >  Keymaze300Reader::getTracks()
{
    return lst_trks;
}

int Keymaze300Reader::getNumTracks()
{
    return lst_trks.count();
}

